package com.ag.improvedjava.iterable;

import java.util.Iterator;
import java.util.NoSuchElementException;

final public class RepeatIterable<T> implements Iterable<T> {

	private T element;
	private int count;

	public RepeatIterable(T element, int count) {
		this.element = element;
		this.count = count;
	}

	@Override
	public Iterator<T> iterator() {
		return new RepeatIterator<T>(element, count);
	}

}

class RepeatIterator<T> extends UnmodifiableIterator<T> {

	private T element;
	private int count;
	
	public RepeatIterator(T element, int count) {
		this.element = element;
		this.count = count;
	}

	@Override
	public boolean hasNext() {
		return count > 0;
	}

	@Override
	public T next() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		count--;
		return element;
	}

}